package top.yangguangmc.smart_scheduling;

import org.jetbrains.annotations.Contract;
import top.yangguangmc.smart_scheduling.pojo.Employee;
import top.yangguangmc.smart_scheduling.pojo.Shift;
import top.yangguangmc.smart_scheduling.utils.Utils;

import java.util.*;

public class SessionManager {
    private final Set<Employee> employees = new HashSet<>();
    private final Set<Shift> shifts = new HashSet<>();

    public SessionManager() {
        addDefaultValues();

    }
    private void addDefaultValues(){
        employees.add(new Employee(1, "张三"));
        employees.add(new Employee(2, "李四"));
        employees.add(new Employee(3, "王五"));
        employees.add(new Employee(4, "赵六"));
        shifts.add(Shift.EMPTY);
        shifts.add(new Shift("节"));
        shifts.add(new Shift("上"));
        shifts.add(new Shift("夜"));
        shifts.add(new Shift("医"));
        shifts.add(new Shift("责1"));
        shifts.add(new Shift("责2"));
        shifts.add(new Shift("责3"));
        shifts.add(new Shift("责4"));
    }

    //
    // Employees managements
    //

    public Set<Employee> getEmployees() {
        return employees;
    }

    @Contract(pure = true)
    public Employee getEmployee(int id) {
        for (Employee emp : employees) {
            if (id == emp.getId())
                return emp;
        }
        return null;
    }

    @Contract(pure = true)
    public Employee getEmployee(String name) {
        if (Utils.isNullOrEmpty(name)) return null;
        for (Employee emp : employees) {
            if (name.equals(emp.getName()))
                return emp;
        }
        return null;
    }

    @Contract(pure = true)
    public String getEmployeesAsString() {
        List<Employee> list = new ArrayList<>(employees);
        list.sort(Comparator.comparingInt(Employee::getId));
        StringBuilder sb = new StringBuilder("[");
        for (Employee emp : list) {
            sb.append(emp.toString());
            sb.append(", ");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.deleteCharAt(sb.length() - 1);
        sb.append("]");
        return sb.toString();
    }

    public boolean addEmployee(Employee employee) {
        String str = employee.getName();
        if (Utils.isNullOrEmpty(str)) return false;
        return employees.add(employee);
    }

    public boolean deleteEmployee(int id) {
        return employees.removeIf(emp -> id == emp.getId());
    }

    public boolean deleteEmployee(String name) {
        if (Utils.isNullOrEmpty(name)) return false;
        return employees.removeIf(emp -> name.equals(emp.getName()));
    }

    //
    // Shifts managements
    //

    public Set<Shift> getShifts() {
        return shifts;
    }

    @Contract(pure = true)
    public Shift getShift(String name) {
        if (Utils.isNullOrEmpty(name)) return null;
        for (Shift shift : shifts) {
            if (name.equals(shift.getName()))
                return shift;
        }
        return null;
    }

    @Contract(pure = true)
    public String getShiftsAsString() {
        List<String> list = new ArrayList<>();
        for (Shift shift : shifts) {
            list.add(shift.getName());
        }
        list.sort(null);
        StringBuilder sb = new StringBuilder("[");
        for (String name : list) {
            sb.append(name);
            sb.append(", ");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.deleteCharAt(sb.length() - 1);
        sb.append("]");
        return sb.toString();
    }

    public boolean addShift(Shift shift) {
        String str = shift.getName();
        if (Utils.isNullOrEmpty(str)) return false;
        return shifts.add(shift);
    }

    public boolean deleteShift(String name) {
        if (Utils.isNullOrEmpty(name)) return false;
        return shifts.removeIf(shift -> name.equals(shift.getName()));
    }

}
